import { WsConnection } from "tsrpc";
import { ecs } from "../../core/ecs/ECS";
import { RoomModelComp } from "./model/RoomModel";
import { RoomJoinComp } from "./system/RoomJoinSystem";
import { ServiceType } from "../../tsrpc/shared/protocols/RoomServiceProto";
import { rs } from "../../RoomServiceEntra";

// /** 房间服务器中的客户端 WebSocket 连接类型 */
// export type RoomConnection = WsConnection<ServiceTypeRoom> & {
//     /** 游戏房间对象 */
//     room: Room;
//     /** 游戏玩家对象 */
//     role: Role;
// };

@ecs.register(`Room`)
export class Room extends ecs.Entity {
    private static RoomCount:number = 0;

    RoomModel:RoomModelComp = null!;
    protected init() {
        this.addComponents<ecs.Comp>(RoomModelComp);
        this.RoomModel.roomId = Room.RoomCount++;
    }


    start() {
        
    }

    join(uId:number) {
        const comp = this.add(RoomJoinComp);
        comp.uId = uId;
    }

    /** 添加房间中玩家 */
    addRole(conn: WsConnection<ServiceType>, uId:number) {
        this.RoomModel.players.push(uId);
        this.RoomModel.conns.push(conn);
        //return RoomRoleUtil.addRole(this, conn, user);
    }

    /** 移除房间中玩家 */
    removeRole(conn: WsConnection<ServiceType>) {
        //RoomRoleUtil.removeRole(conn);
    }

    /** 房间内消息广播 */
    broadcastMsg<T extends keyof ServiceType['msg']>(msgName: T, msg: ServiceType['msg'][T]) {
        rs.RoomServiceModel.wsService.broadcastMsg(msgName, msg, this.RoomModel.conns);
    }
}